const dayjs = require('dayjs')
import { Loading } from 'element-ui'

let loadingInstance = null
const publicUtils = {}
publicUtils.install = function (Vue, opt) {
  // XXXXX-XXXXX-XXXXX-XXXXX-XXXXX 格式GUID生成

  const { ebus } = opt
  Vue.prototype.$EVBUS = ebus

  function _guid() {
    function N() {
      return ((Math.random() * 0x1000000) | 0).toString(16).toLocaleUpperCase().substring(0, 5)
    }
    return `${N()}-${N()}-${N()}-${N()}-${N()}`
  }

  function _curry(fn) {
    return function curryFun(...args) {
      if (args.length < fn.length) {
        return function () {
          return curryFun(...args, ...arguments)
        }
      } else {
        return fn(...args)
      }
    }
  }

  function _showLoading(option) {
    // if (loadingInstance != null) loadingInstance.close()
    let options = {
      lock: true,
      text: '加载数据中，请稍等',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)',
    }
    // 不直接赋值，减少配置内容
    if (option) {
      Object.keys(option).forEach((key) => {
        options[key] = option[key]
      })
    }
    // if (option) options = option
    loadingInstance = Loading.service(options)
  }

  function _closeLoading() {
    if (loadingInstance != null) loadingInstance.close()
  }

  Vue.prototype.$ShowLoading = _showLoading
  Vue.prototype.$CloseLoading = _closeLoading

  Vue.prototype.$guid = _guid
  Vue.prototype.$dayjs = dayjs
}
export default publicUtils
